<template>
	<view class="corrigendumBuilding">
		<view class="corrigendumBuilding-content ff-bg-fff ff-font-14 ff-color-222 ff-margin-12 ff-border-radius-10">
			<view class="ff-font-16 ff-padding-15 ff-font-weight-600 ff-border-bottom">勘误建筑物</view>
			<view class="ff-flex-between ff-align-items-one ff-border-bottom ff-padding-15">
				<view class="corrigendumBuilding-content-title ff-color-999">建筑高度(米)</view>
				<input class="ff-text-right" placeholder="请输入" placeholder-style="color:#999" type="digit"
					v-model.trim="params.buildingHeight" />
			</view>
			<view class="ff-flex-between ff-align-items-one ff-border-bottom ff-padding-15">
				<view class="corrigendumBuilding-content-title ff-color-999">建筑物面积(m²)</view>
				<input class="ff-text-right" placeholder="请输入" placeholder-style="color:#999" type="digit"
					v-model.trim="params.buildingArea" />
			</view>
			<view class="ff-flex-between ff-align-items-one ff-border-bottom ff-padding-15">
				<view class="corrigendumBuilding-content-title ff-color-999">建筑地上层数(层)</view>
				<input class="ff-text-right" placeholder="请输入" placeholder-style="color:#999" type="number"
					v-model.trim="params.buildingUpLayers" />
			</view>
			<view class="ff-flex-between ff-align-items-one ff-border-bottom ff-padding-15">
				<view class="corrigendumBuilding-content-title ff-color-999">建筑地下层数(层)</view>
				<input class="ff-text-right" placeholder="请输入" placeholder-style="color:#999" type="number"
					v-model.trim="params.buildingDownLayers" />
			</view>
		</view>
		<view class="corrigendumBuilding-but ff-flex-between">
			<view class="corrigendumBuilding-but-list"><u-button shape="circle" @click="cancelBuildsClick"
					plain>取消</u-button></view>
			<view class="corrigendumBuilding-but-list"><u-button type="primary" shape="circle"
					@click="upateBuildsClick">保存</u-button></view>
		</view>
		<!-- 弹窗区 -->
		<u-toast ref="uToast" />
		<f-loading ref="loading" />
	</view>
</template>

<script>
	export default {
		name: 'corrigendumBuilding',
		data() {
			return {
				paramsInfo: {}, // 前页面传的数据
				detailsUrl: '', // 获取建筑物详情的地址
				submitUrl: '', // 提交建筑物的地址
				// 建筑物提交的数据
				params: {
					id: '',
					buildingHeight: '', // 单体建筑高度(单位米，默认保留两位小数)
					buildingArea: '', // 建筑物面积(m²)
					buildingUpLayers: '', // 建筑地上层数(单位层)
					buildingDownLayers: '' // 建筑地下层数(单位层)
				}
			};
		},
		onLoad(e) {
			// console.log('前页面传过来的数据------', e);
			let paramsInfo = JSON.parse(e.paramsInfo);
			this.paramsInfo = paramsInfo
			this.params.id = this.paramsInfo.maintenanceBuildingId;
			if (this.paramsInfo.type == 1) {
				this.detailsUrl = `/serviceOrg/maintenance/buildings/${this.paramsInfo.maintenanceId}`; // 第一次勘误建筑物详情接口
				this.submitUrl = '/serviceOrg/project/buildings/info/updateV2'; // 第一次建筑物勘误提交接口
				this.getBuildDetails(); // 获取建筑物详情数据
				return;
			}
			if (this.paramsInfo.type == 2) {
				this.detailsUrl = `/serviceOrg/project/buildings/${this.paramsInfo.maintenanceId}`;
				this.submitUrl = '/serviceOrg/project/buildings/info/update';
				this.getBuildDetails(); // 获取建筑物详情数据
				return;
			}
		},
		methods: {
			// 获取建筑物详情数据
			async getBuildDetails() {
				this.$refs.loading.openLoading('加载中···'); // 打开loading动画
				const data = await this.$http.get({
					url: this.detailsUrl
				});
				// console.log('获取建筑物详情数据------', data);
				if (data.code == 200) {
					const buildDetails = data.data[0];
					this.params.buildingHeight = buildDetails.buildingHeight;
					this.params.buildingUpLayers = buildDetails.buildingUpLayers;
					this.params.buildingDownLayers = buildDetails.buildingDownLayers;
					this.params.buildingArea = buildDetails.buildingArea;
				}
				this.$refs.loading.closeLoading(); // 关闭loading动画
			},
			// 取消---返回上一层
			cancelBuildsClick() {
				this.$pageJump.navigateBack();
			},
			// 保存-----确定操作
			upateBuildsClick() {
				let that = this;
				if (!that.$check.numberCheckPoint(that.params.buildingHeight)) {
					that.$refs.uToast.show({
						title: '建筑物高度不能为负数，且允许保留小数点后2位',
						type: 'error'
					});
					return;
				}
				if (!that.$check.numberCheckPoint(that.params.buildingArea)) {
					that.$refs.uToast.show({
						title: '建筑面积不能为负数，且允许保留小数点后2位',
						type: 'error'
					});
					return;
				}
				if (!that.$check.numberCheck(that.params.buildingUpLayers)) {
					that.$refs.uToast.show({
						title: '请正确填写建筑地上层数（0及0以上的整数)',
						type: 'error'
					});
					return;
				}
				if (!that.$check.numberCheck(that.params.buildingDownLayers)) {
					that.$refs.uToast.show({
						title: '请正确填写建筑地下层数（0及0以上的整数)',
						type: 'error'
					});
					return;
				}
				that.$refs.loading.openLoading('提交中···'); // 打开loading动画
				that.$http.post({
					url: that.submitUrl,
					data: that.params
				}).then(res => {
					// console.log('建筑物信息---保存返回的结果', res);
					that.$refs.loading.closeLoading(); // 关闭loading动画
					if (res.code == 200) {
						that.$refs.uToast.show({
							title: '保存成功',
							type: 'success'
						});
						setTimeout(() => {
							// 刷新前一个页面 （配合前页面的onShow使用）
							uni.navigateBack({
								delta: 1,
								success: function() {
									let page = getCurrentPages().pop(); //跳转页面成功之后
									if (!page) return;
									page.onLoad(page.options); //如果页面存在，则重新刷新页面
								}
							});
						}, 500);
					} else if (res.code == 5001) {
						that.$refs.uToast.show({
							title: res.data[0],
							type: 'error'
						});
					} else {
						that.$refs.uToast.show({
							title: res.msg,
							type: 'error'
						});
					}
				});
			}
		},
		onShareAppMessage() {
			return {
				title: '分享小程序给朋友',
				path: '/pages/homePage/Home'
			};
		}
	};
</script>

<style lang="scss" scoped>
	.corrigendumBuilding {
		&-but {
			&-list {
				width: 50%;
				margin: 15% 3%;

				/deep/.u-size-default {
					height: 90rpx !important;
					line-height: 90rpx !important;
					color: #95979e;
					border-color: #95979e;
				}

				/deep/.u-btn--primary {
					color: #ffffff !important;
					border-color: #3178ff !important;
					background-color: #3178ff !important;
				}
			}
		}
	}
</style>